$("#form-customerClue-add").validate({
	rules : {
		productType : {
			required : true,
		},
		customerName : {
			required : true,
		},
		customerCellphone : {
			required : true,
		}
	},
	submitHandler : function(form) {
		add();
	}
});

/*function add() {
	_ajax_save(ctx + "module/customerClue/save", $('#form-customerClue-add')
			.serialize());
}*/

function add() {
	var productType = $("select[name='productType'] option:selected").text();		//线索产品类型
	var clueOperationType = $("input[name='clueOperationType']").val();
	var typeId = $("select[name='productType']").val();
	var clueSource = $("input[name='clueSource']").val();							//线索来源
	var isHandle = $("input[name='isHandle']").val();								//是否处理
	var clueChannel = $("select[name='clueChannel']").val();						//线索来源渠道
	var customerName = $("input[name='customerName']").val();						//客户姓名
	var customerBackground = $("select[name='customerBackground']").val();			//客户背景
	var customerCellphone = $("input[name='customerCellphone']").val();				//手机号码
	var customerSex = $("input[name='customerSex']").val();							//性别
	var customerAge = $("input[name='customerAge']").val();							//年龄
	var customerEducation = $("select[name='customerEducation']").val();			//客户学历
	var customerBirth = $("input[name='customerBirth']").val();						//出生日期
	var customerJob = $("select[name='customerJob']").val();						//客户职业
	var customerIdentityNum = $("input[name='customerIdentityNum']").val();			//身份证号码
	var customerPhone = $("input[name='customerPhone']").val();						//电话号码
	var customerWechatAccount = $("input[name='customerWechatAccount']").val();		//微信号
	var customerEmergencyContact = $("input[name='customerEmergencyContact']").val();//紧急联系人
	var customerEmergencyCellphone = $("input[name='customerEmergencyCellphone']").val();//紧急联系人手机号码
	var customerProvince = $("select[name='customerProvince']").val();				//省
	var customerCity = $("select[name='customerCity']").val();						//市
	var customerArea = $("select[name='customerArea']").val();						//区
	var customerAddressDetail = $("input[name='customerAddressDetail']").val();		//详细地址
	var productIds = document.getElementById("productType").selectedIndex;			//获得商品类型下拉是第几个被选中了
	var recureProject = $("select[name='kfProductId'] option:selected").text();		//康复医院
	var tourismRoute = $("select[name='lyProductId'] option:selected").text();		//旅游路线
	var tourismScale = $("input[name='tourismScale']").val();						//抱团人数
	var occupancyType = $("input[name='occupancyType']").val();						//入住类型
	var lyProductId = $("select[name='lyProductId']").val();
	var kfProductId = $("select[name='kfProductId']").val();
	if(productIds == 1){
		var productId = lyProductId;
		recureProject = '';
		occupancyType = 0;
	}else if(productIds == 2){
		var productId = kfProductId;
		tourismRoute = '';
		tourismScale = 0;
		occupancyType = 0;
	}else if(productIds == 3){
		var productId = '';
		recureProject = '';
		tourismRoute = '';
		tourismScale = 0;
	}else{
		var productId = '';
		recureProject = '';
		tourismRoute = '';
		tourismScale = 0;
		occupancyType = 0;
	}
	var myreg=/^[1][3,4,5,7,8][0-9]{9}$/; //手机号码
	if(!myreg.test(customerCellphone)){
		layer.msg('请输入正确的手机号码')
		return false
	}else{
		$.ajax({
			cache : true,
			type : "POST",
			url : ctx + "module/customerClue/save",
			data : {
				"productType": productType,
				"clueOperationType": clueOperationType,
				"typeId": typeId,
				"clueSource": clueSource,
				"isHandle": isHandle,
				"clueChannel": clueChannel,
				"customerName": customerName,
				"customerBackground": customerBackground,
				"customerCellphone": customerCellphone,
				"customerSex": customerSex,
				"customerAge": customerAge,
				"customerEducation": customerEducation,
				"customerBirth": customerBirth,
				"customerJob": customerJob,
				"customerIdentityNum": customerIdentityNum,
				"customerPhone": customerPhone,
				"customerWechatAccount": customerWechatAccount,
				"customerEmergencyContact": customerEmergencyContact,
				"customerEmergencyCellphone": customerEmergencyCellphone,
				"customerProvince": customerProvince,
				"customerCity": customerCity,
				"customerArea": customerArea,
				"customerAddressDetail": customerAddressDetail,
				"recureProject": recureProject,
				"tourismRoute": tourismRoute,
				"tourismScale": tourismScale,
				"occupancyType": occupancyType,
				"productId": productId,
			},
			async : false,
			error : function(request) {
				$.modalAlert("系统错误", "error");
			},
			success : function(data) {
				if (data.code == 0) {
					parent.layer.msg("新增成功,正在刷新数据请稍后……",{icon:1,time: 500,shade: [0.1,'#fff']},function(){
						$.parentReload();
					});
				} else {
					$.modalAlert(data.msg, "error");
				}

			}
		});	
	}
}

window.onload = function(){ 
　　　　changeSelect();

} 

function changeSelect() {
	var productId = document.getElementById("productType").selectedIndex;// 获得是第几个被选中了
	var select = document.getElementById("productType");
	var options = select.getElementsByTagName("option");
	for (var i = 0; i < options.length; i++) {
		if (productId == i) {
			var ss = $(".diss"+i);
			$(ss).show();
		} else {
			var sa = $(".diss"+i);
			$(sa).hide();
		}
	}
}

$(function() {
	var html = "";
	$("#input_city").append(html);
	$("#input_area").append(html);
	$.each(pdata, function(idx, item) {
		if (parseInt(item.level) == 0) {
			html += "<option value='" + item.names + "' exid='" + item.code
					+ "'>" + item.names + "</option>";
		}
	});
	$("#input_province").append(html);
	$("#input_province").change(
			function() {
				if ($(this).val() == "")
					return;
				$("#input_city option").remove();
				$("#input_area option").remove();
				var code = $(this).find("option:selected").attr("exid");
				code = code.substring(0, 2);
				var html = "<option value=''>--请选择--</option>";
				$("#input_area").append(html);
				$.each(pdata, function(idx, item) {
					if (parseInt(item.level) == 1
							&& code == item.code.substring(0, 2)) {
						html += "<option value='" + item.names + "' exid='"
								+ item.code + "'>" + item.names + "</option>";
					}
				});
				$("#input_city").append(html);
			});
	$("#input_city").change(
			function() {
				if ($(this).val() == "")
					return;
				$("#input_area option").remove();
				var code = $(this).find("option:selected").attr("exid");
				code = code.substring(0, 4);
				var html = "<option value=''>--请选择--</option>";
				$.each(pdata, function(idx, item) {
					if (parseInt(item.level) == 2
							&& code == item.code.substring(0, 4)) {
						html += "<option value='" + item.names + "' exid='"
								+ item.code + "'>" + item.names + "</option>";
					}
				});
				$("#input_area").append(html);
			});
});